const { or } = require('sequelize');
const { getOrderById } = require('../service/order.service');
const ordervalidator = async (ctx, next) => {
    //订单数据格式是否为空验证
    const {order_admin,order_name, order_id, order_price, order_status,order_phone,order_address,order_province,order_remark,order_goodslist}= ctx.request.body;
    console.log(order_admin,order_name, order_id, order_price, order_status,order_phone,order_address,order_province,order_remark,order_goodslist)

    if (!order_admin ||!order_name ||!order_id ||!order_price ||!order_status||!order_phone||!order_address||!order_province||!order_remark) {
          ctx.throw(400, '订单数据部分为空');
         return
    } else if (typeof order_admin !== "string"|| typeof order_name !== "string"|| typeof order_id !== "string"||typeof order_price !== "number"|| typeof order_status !== "number",typeof order_phone !== "number"|| typeof order_address !== "string"|| typeof order_province !== "string"|| typeof order_remark !== "string"){
         ctx.throw(400, '订单数据类型格式错误');
         return}
        
    for(let i=0;i<order_goodslist.length;i++){
        //商品数据格式是否为空验证
        if( !order_goodslist[i].goods_id ||!order_goodslist[i].goods_name  ||!order_goodslist[i].goods_num){
            ctx.throw(400, '商品数据为空');
            return
        }

        else if( typeof order_goodslist[i].goods_id !== "string"|| typeof order_goodslist[i].goods_name !== "string"|| typeof order_goodslist[i].goods_num !== "number"){
            ctx.throw(400, '商品数据类型格式错误');
            return
        }
    }

     await next();
}
//订单编号验证
const verifyid = async (ctx, next) => {
    const {order_id} = ctx.request.body;
    const order = await getOrderById(order_id);
    if (order) {
         ctx.throw(400, '订单已存在');    
        return
    }
    await next();}
const verifyorder_id = async (ctx, next) => {
        

        const order = await getOrderById(ctx.params.id);
        
         if (!order) {
             ctx.throw(400, '订单不存在');    
            return
        }
        await next();}


module.exports = {ordervalidator,verifyid,verifyorder_id}